<?php 
namespace app\dboard\logic;
use think\Db;
use think\facade\Session;
class Courseware extends Base
{

    function __construct()
    {
    }

    public function updateCoursewareauthorize($param)
    {
        $course_ids = isset($param['course_ids']) ? $param['course_ids']:0;
        $sids = isset($param['sids']) ? intval($param['sids']):0;
        $tids = isset($param['tids']) ? intval($param['tids']):0;
        $start_time = isset($param['start_time']) ? trim($param['start_time']):'';
        $end_time = isset($param['end_time']) ? trim($param['end_time']):'';
        if (!$tids || !$course_ids || !$start_time || !$end_time) {
            $this->res_data['Msg'] = "缺少参数";
            return $this->res_data;
        }
        if (!is_array($course_ids)) {
            $this->res_data['Msg'] = "参数格式不正确";
            return $this->res_data;
        }
        $teacher = Db::name('teacher')->where(['id'=>$tids,'status'=>1])->field('id')->find();
        if (!$teacher) {
            $this->res_data['Msg'] = "教师不存在";
            return $this->res_data;
        }
        $start_time = strtotime($start_time);
        if ($start_time<0) {
            $this->res_data['Msg'] = "授权开始时间不正确";
            return $this->res_data;
        }
        $end_time = strtotime($end_time);
        if ($end_time<0) {
            $this->res_data['Msg'] = "授权结束时间不正确";
            return $this->res_data;
        }
        Db::startTrans();
        // try {
            // $course_ids_arr = explode(',', trim($course_ids,','));
            if (Db::name('courseware_authorize')->where(['teacher_id'=>$tids,'status'=>1,'type'=>2])->find()) {
                $is_update = Db::name('courseware_authorize')->where(['teacher_id'=>$tids,'status'=>1,'type'=>2])->update(['status'=>0,'update_time'=>time()]);
                if (!$is_update) {
                    Db::rollBack();
                    $this->res_data['Msg'] = "授权出错";
                    return $this->res_data;
                }
            }
            $data = [];
            foreach ($course_ids as $key1 => $value1) {
                $data[$key1]['course_id'] = $value1;
                $data[$key1]['type'] = 2;
                $data[$key1]['teacher_id'] = $tids;
                $data[$key1]['status'] = 1;
                $data[$key1]['create_time'] = time();
                $data[$key1]['start_time'] = $start_time;
                $data[$key1]['end_time'] = $end_time;
            }
            $is_insert = Db::name('courseware_authorize')->insertAll($data);
            if (!$is_insert) {
                Db::rollBack();
                $this->res_data['Msg'] = "授权出错";
                return $this->res_data;
            }
            Db::commit();
            $this->res_data['Msg'] = '成功';
            $this->res_data['Code'] = 0;
            return $this->printResult($this->res_data);
        // } catch (\Exception $e) {
        //     Db::rollBack();
        //     $this->res_data['Msg'] = "授权出错";
        //     return $this->res_data;
        // }
        $this->res_data['Msg'] = '成功';
        $this->res_data['Code'] = 0;
        return $this->printResult($this->res_data);
    }

    // public function updateCoursewareauthorize($param,$sid)
    // {
    //     $id = isset($param['id']) ? intval($param['id']):0;
    //     $ids = isset($param['ids']) ? trim($param['ids']):'';
    //     if (!$ids || !$id) {
    //         $this->res_data['Msg'] = "缺少参数";
    //         return $this->res_data;
    //     }
    //     Db::startTrans();
    //     try {
    //         $ids_arr = explode(',', trim($ids,','));
    //         foreach ($ids_arr as $key => $value) {
    //             $teacher = Db::name('teacher')->where(['id'=>$value,'status'=>1,'sid'=>$sid])->field('id')->find();
    //             if (!$teacher) {
    //                 Db::rollBack();
    //                 $this->res_data['Msg'] = "老师不存在";
    //                 return $this->res_data;
    //             }
    //         }
    //         if (Db::name('courseware_authorize')->where(['courseware_id'=>$id,'status'=>1,'type'=>2])->find()) {
    //             $is_update = Db::name('courseware_authorize')->where(['courseware_id'=>$id,'status'=>1,'type'=>2])->update(['status'=>0,'update_time'=>time()]);
    //             if (!$is_update) {
    //                 Db::rollBack();
    //                 $this->res_data['Msg'] = "授权出错";
    //                 return $this->res_data;
    //             }
    //         }
    //         $data = [];
    //         foreach ($ids_arr as $key1 => $value1) {
    //             $data[$key1]['sid'] = $sid;
    //             $data[$key1]['courseware_id'] = $id;
    //             $data[$key1]['teacher_id'] = $value1;
    //             $data[$key1]['status'] = 1;
    //             $data[$key1]['type'] = 2;
    //             $data[$key1]['create_time'] = time();
    //         }
    //         $is_insert = Db::name('courseware_authorize')->insertAll($data);
    //         if (!$is_insert) {
    //             Db::rollBack();
    //             $this->res_data['Msg'] = "授权出错";
    //             return $this->res_data;
    //         }
    //         Db::commit();
    //         $this->res_data['Msg'] = '成功';
    //         $this->res_data['Code'] = 0;
    //         return $this->printResult($this->res_data);
    //     } catch (\Exception $e) {
    //         Db::rollBack();
    //         $this->res_data['Msg'] = "授权出错";
    //         return $this->res_data;
    //     }
    //     $this->res_data['Msg'] = '成功';
    //     $this->res_data['Code'] = 0;
    //     return $this->printResult($this->res_data);
    // }

}

?>
